<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="ID卡号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operid">
              <a-input v-model="model.operid" placeholder="ID卡号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入描述" style="width: 100%"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <a-select
                ref="select"
                placeholder="请选择状态"
                v-model="model.status">
                <a-select-option value="1">可用</a-select-option>
                <a-select-option value="0">不可用</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

import { httpAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'MesOperatorModal',
  components: {},
  props: {
    mainId: {
      type: String,
      required: false,
      default: ''
    }
  },
  data() {
    return {
      isEdit: false,
      title: '操作',
      width: 800,
      visible: false,
      userid: '',
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        itempartno: [
          { required: true, message: '请输入物料!' }
        ],
        qtyPer: [
          { required: true, message: '请输入消耗数量!' }
        ],
        op: [
          { required: true, message: '请输入需要上料的工序!' }
        ],
        lottrack: [
          { required: true, message: '请输入是否需要扣减物料批次!' }
        ]
      },
      url: {
        add: '/sys/mesOperator/add',
        edit: '/sys/mesOperator/edit'
      }

    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add(model) {
      this.isEdit = false
      console.log('新增接受')
      console.log(model)
      this.userid = model.username
      this.edit(model)
    },
    edit(record) {
      console.log('修改接受')
      console.log(record)
      //this.model = Object.assign({}, record)
      if (record.operid) {
        this.isEdit = true
      }
      this.userid = record.username
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.model = {}
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          this.model.pwd = '1'
          this.model.userid = this.userid
          that.$emit('ok', this.model)
          that.confirmLoading = false
          that.close()
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }

  }
}
</script>
